//
//  AppDelegate.m
//  PushNoti
//
//  Created by lizhongren on 15/10/20.
//  Copyright © 2015年 lizhongren. All rights reserved.
//

#import "AppDelegate.h"
#import "XGPush.h"
#import "XGSetting.h"

#define _IPHONE80_ 80000


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    [XGPush startApp:2200154573 appKey:@"I428RMCFF82M"];

    application.applicationIconBadgeNumber = 0;
    // 判断系统版本是否大于等于8.0
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        // 如果大于调用8.0的方法
        // 设置远程通知配置
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];
        // 注册远程通知
        [application registerForRemoteNotifications];
    } else {
        
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
        
    }
    
    
    return YES;
}

// 注册成功时调用
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // 信鸽推送, 注册设备
    [XGPush registerDevice:deviceToken];
    NSLog(@"%@", deviceToken);
    // 在这里, 去向服务器发送deviceToken
}

// 注册失败, 回调方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"注册失败 error = %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // userInfo字典中存储了关于推送的信息
    // 以及我App需要的id等
    NSLog(@"%@", [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
    application.applicationIconBadgeNumber = 0;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil];
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
    application.applicationIconBadgeNumber = 0;

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
